import time
import uuid

from selenium import webdriver
from selenium.webdriver.common.by import By

class CFS:
    def __init__(self ,path = 'http://localhost:90' ,status = 0):
        self.path = path
        if status == 0:
            self.wB = webdriver.Chrome()
        else:
            self.wB = webdriver.Firefox()
        self.wB.get(path)
        self.wB.maximize_window()

    def regist(self ,newUsername ,newPassword):
        self.wB.get('http://localhost:90/toregister.html')
        self.wB.find_element(By.ID ,'username').send_keys(newUsername)
        self.wB.find_element(By.ID ,'password').send_keys(newPassword)
        self.wB.find_element(By.ID ,'repassword').send_keys(newPassword)
        self.wB.find_element(By.ID ,'login_btn').click()
        self.wB.implicitly_wait(10)
        return self.wB.title

    def login(self ,username ,password):
        self.wB.find_element(By.ID ,'username').send_keys(username)
        self.wB.find_element(By.ID ,'password').send_keys(password)
        self.wB.find_element(By.ID ,'login_btn').click()
        self.wB.implicitly_wait(10)
        return self.wB.title

    def onlineloan(self ,amount ,day):
        self.wB.get('http://localhost:90/user/tools/toApplyLoan.html')
        self.wB.find_element(By.ID ,'amount').send_keys(amount)
        self.wB.find_element(By.ID ,'term').send_keys(day)
        self.wB.find_element(By.ID ,'submit').click()
        self.wB.find_element(By.XPATH ,'//div[@class="jconfirm-buttons"]//button[@class="btn btn-primary"]').click()
        return self.wB.find_element(By.XPATH ,'/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/span').text

    def addBankcardmanagement(self ,cardName ,cardNum):
        self.wB.get('http://localhost:90/user/personal/toBankCard.html')
        oldNum = len(self.wB.find_elements(By.CLASS_NAME, 'card-body'))
        self.wB.find_element(By.ID ,'bankCard_add_modal_btn').click()
        self.wB.find_element(By.ID ,'cardBank_add_input').send_keys(cardName)
        self.wB.find_element(By.ID ,'cardNum_add_input').send_keys(cardNum)
        self.wB.find_element(By.ID ,'bankCard_save_btn').click()
        self.wB.get('http://localhost:90/user/personal/toBankCard.html')
        return len(self.wB.find_elements(By.CLASS_NAME ,'card-body')) - oldNum

    def updateBankcardmanagement(self ,cardName = None ,cardNum = None):
        self.wB.get('http://localhost:90/user/personal/toBankCard.html')
        self.wB.find_element(By.XPATH ,'/html/body/div/div/main/div/div/div[3]/div/div/div[1]/ul/li/button[1]').click()
        self.wB.implicitly_wait(10)
        if cardName != None:
            self.wB.find_element(By.ID ,'cardBank_update_input').clear()
            self.wB.find_element(By.ID ,'cardBank_update_input').send_keys(cardName)
        if cardNum != None:
            self.wB.find_element(By.ID ,'cardNum_update_input').send_keys(cardNum)
        self.wB.find_element(By.ID ,'bankCard_update_btn').click()
        self.wB.get('http://localhost:90/user/personal/toBankCard.html')
        return self.wB.find_element(By.XPATH ,'/html/body/div/div/main/div/div/div[3]/div/div/div[1]/div/h4').text.split()[0]

    def smallChangeFinancialManagement(self ,password = '666666'):
        self.wB.get('http://localhost:90/user/finance/toChangeMoney.html')
        self.wB.find_element(By.XPATH ,'/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button').click()
        # 这个为啥不能直接用id
        self.wB.find_element(By.XPATH ,'/html/body/div[3]/div[2]/input').send_keys(password)
        self.wB.find_element(By.CLASS_NAME ,'layui-layer-btn0').click()

    def oTermFinancial(self ,password = '666666'):
        self.wB.get('http://localhost:90/user/finance/toTermFinancial.html')
        self.wB.find_element(By.XPATH ,'/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[8]/button').click()
        self.wB.find_element(By.XPATH ,'/html/body/div[3]/div[2]/input').send_keys(password)
        self.wB.find_element(By.CLASS_NAME ,'layui-layer-btn0').click()

    def jieping(self, mokuai):
        fileName = str(uuid.uuid4())
        if mokuai == 1:
            self.wB.save_screenshot('/CFS1/'+fileName+'.png')
        elif mokuai == 2:
            self.wB.save_screenshot('/CFS2/' + fileName + '.png')
        elif mokuai == 3:
            self.wB.save_screenshot('/CFS3/' + fileName + '.png')
        elif mokuai == 4:
            self.wB.save_screenshot('/CFS4/' + fileName + '.png')
        elif mokuai == 5:
            self.wB.save_screenshot('/CFS5/' + fileName + '.png')

    def __del__(self):
        self.wB.quit()
